package system.schedule;

import java.util.ArrayList;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.apep.connect.APEPRequest;
import com.apep.connect.APEPResponse;
import com.apep.lib.xml.SampleElement;
import com.hitrust.gs.bean.GSManufactureSyncBean;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.integration.socket.GSGwbsClient;
import com.hitrust.gs.lib.GSLogWriter;

public class ProductUploadTask implements Job{

	private static boolean isRunning = false;
	public static String ROOT="data";
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		if(!isRunning){
			myRun();
		}else{
			System.out.println("-----------------------------");
		}	
	}

	private void myRun() {
		isRunning = true;
		try {
			GSManufactureDAO dao = new GSManufactureDAO();
			ArrayList list=dao.getProductSyncData();
				for(int i=0;i<list.size();i++){
					GSManufactureSyncBean bean=(GSManufactureSyncBean)list.get(i);
					SampleElement element=new SampleElement(ROOT);
					element.addChildElement(new SampleElement("id",bean.getId()));
					element.addChildElement(new SampleElement("concentricityCode",bean.getGs_concentricity()));
					element.addChildElement(new SampleElement("apepCode",bean.getGs_manufacture_id()));
					element.addChildElement(new SampleElement("name",bean.getGs_manufacture_name()));
					element.addChildElement(new SampleElement("categoryCode",bean.getGs_manufacture_category_id()));
					element.addChildElement(new SampleElement("productBrand",bean.getGs_manufacture_brand()));
					element.addChildElement(new SampleElement("model",bean.getGs_manufacture_type()));
					element.addChildElement(new SampleElement("spec",bean.getGs_manufacture_spec()));
					element.addChildElement(new SampleElement("unitPrice",bean.getGs_manufacture_unitprice()+""));
					element.addChildElement(new SampleElement("productUnit",bean.getGs_quant_unit()));
					element.addChildElement(new SampleElement("qualityAssurePeriod",bean.getGs_quality_assure_period()));
					element.addChildElement(new SampleElement("manufaturer",bean.getGs_manufacturer_name()));
					element.addChildElement(new SampleElement("area",bean.getGs_make_area()));
					element.addChildElement(new SampleElement("providerCode",bean.getGs_provider_id()));
					element.addChildElement(new SampleElement("adaptMachineType",bean.getGs_adapt_machine_type()));
					element.addChildElement(new SampleElement("recentAdjustPrice",bean.getGs_recent_adjusted_price()+""));
					element.addChildElement(new SampleElement("recentAdjustPriceDate",bean.getGs_recent_adjustprice_date()));
					element.addChildElement(new SampleElement("recorderId",bean.getGs_recorder_id()));
					element.addChildElement(new SampleElement("recorderDate",bean.getGs_manufacture_record_date()));
					element.addChildElement(new SampleElement("isTradeFlatProduct",bean.getGs_is_tradeflat_manufacture()));
					element.addChildElement(new SampleElement("appendProperty",bean.getGs_appender_property()));
					element.addChildElement(new SampleElement("remark",bean.getGs_remark()));
					element.addChildElement(new SampleElement("drawingPieceCode",bean.getGs_drawing_peice_code()));
					element.addChildElement(new SampleElement("balanceMode",bean.getGs_balance_mode()));
					element.addChildElement(new SampleElement("isNormal",bean.getIs_normal()));
					element.addChildElement(new SampleElement("warehouseCode",bean.getWarehouse_code()));
					element.addChildElement(new SampleElement("warehouseName",bean.getWarehouse_name()));
					element.addChildElement(new SampleElement("isBuy",bean.getIs_buy()));
					element.addChildElement(new SampleElement("status",bean.getGs_validity()));
					element.addChildElement(new SampleElement("createUserId",bean.getCreate_user_id()));
					element.addChildElement(new SampleElement("createUser",bean.getCreate_user()));
					element.addChildElement(new SampleElement("createTime",bean.getCreate_time()));
					element.addChildElement(new SampleElement("lastUpdateUserId",bean.getLast_update_user_id()));
					element.addChildElement(new SampleElement("lastUpdateUser",bean.getLast_update_user()));
					element.addChildElement(new SampleElement("lastUpdateTime",bean.getLast_update_time()));
					element.addChildElement(new SampleElement("isUpdate",bean.getIs_update()));
					element.addChildElement(new SampleElement("planPrice",bean.getPlan_price()+""));
					element.addChildElement(new SampleElement("kmQualityAssurePeriod",bean.getKm_quality_assure_period()));
					element.addChildElement(new SampleElement("productType",bean.getProduct_type()));
					element.addChildElement(new SampleElement("isBid",bean.getIs_bid()));
					APEPRequest req=new APEPRequest();
					req.setAction("UploadtoGwbs");
					req.setData(element);
					GSGwbsClient cilent=new GSGwbsClient();
					APEPResponse res= cilent.sendToGwbs(req);
					
					if("00".equals(res.get("ResultCode"))) {
						dao.deleteSyncData(bean.getGs_concentricity());
					} else {
						dao.addFailedCount(bean.getGs_concentricity());
					}
					GSLogWriter.info(res.toXml());
				}
		} catch (Exception e) {
			e.printStackTrace();
		}

		isRunning = false;
	}
}
